package com.qf.jie.service.impl;

import com.qf.jie.dao.CommodityDao;
import com.qf.jie.dao.OrderDao;
import com.qf.jie.dao.UserDao;
import com.qf.jie.dao.impl.CommodityDaoImpl;
import com.qf.jie.dao.impl.OrderDaoImpl;
import com.qf.jie.dao.impl.UserDaoImpl;
import com.qf.jie.entity.po.Commodity;
import com.qf.jie.entity.po.Order;
import com.qf.jie.entity.po.User;
import com.qf.jie.entity.vo.RespBean;
import com.qf.jie.service.OrderService;
import com.qf.jie.utils.DBUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

public class OrderServiceImpl implements OrderService {

    OrderDao orderDao = new OrderDaoImpl();
    CommodityDao commodityDao = new CommodityDaoImpl();
    UserDao userDao = new UserDaoImpl();

    // 新增订单，买家支付完成时生成
    @Override
    public RespBean addOrder(HttpServletRequest req, String strCommodity_id, String address, String phone) {
        try{
            long datetime = new Date().getTime();       //时间及时生成
            int commodity_id = Integer.parseInt(strCommodity_id);   //商品id由客户端传来

            User user = (User) req.getSession().getAttribute("user");
            int user_id_0 = user.getId();         //买家id从session获取

            List<Commodity> commodities = commodityDao.getBeanById(commodity_id);

            int user_id_1 = commodities.get(0).getUser_id();         //卖家id通过商品信息获取


            int status = 0;               //初始状态为0，未发货
            Order order = new Order(0, datetime, commodity_id, user_id_0, user_id_1, status, address, phone);
            int num = orderDao.addBean(order);
            if (num == 0){
                return RespBean.respError("新增订单失败，未知错误");
            }

            //订单创建完成，商品的库存 -1
            commodities.get(0).setStock(commodities.get(0).getStock() - 1);
            commodityDao.updateById(commodities.get(0));
            return RespBean.respSuccess("创建订单成功");
        }catch (NumberFormatException e){
            e.printStackTrace();
            return RespBean.respError("商品id有异常");
        }
    }

    // 获取订单（我是买家）
    @Override
    public RespBean getOrderListByUserId_0(HttpServletRequest req) {
        User user = (User)req.getSession().getAttribute("user");
        int user_id_0 = user.getId();
        List<Order> list = orderDao.getOrderListByUserId_0(user_id_0);
        if (list.size() == 0){
            return RespBean.respError("未获取到任何订单信息");
        }
        return RespBean.respSuccess(list);
    }
    // 获取订单（我是卖家）
    @Override
    public RespBean getOrderListByUserId_1(HttpServletRequest req ) {
        User user = (User)req.getSession().getAttribute("user");
        int user_id_1 = user.getId();
        List<Order> list = orderDao.getOrderListByUserId_1(user_id_1);
        if (list.size() == 0){
            return RespBean.respError("未获取到任何订单信息");
        }
        return RespBean.respSuccess(list);
    }

    //发货
    @Override
    public RespBean updateStatusByUser_id_1(HttpServletRequest req, String strOrder_id) {
        try{
            int order_id = Integer.parseInt(strOrder_id);
            int num = orderDao.updateStatusById(1, order_id);
            if (num == 0){
                return RespBean.respError("操作异常，未知错误");
            }
            return RespBean.respSuccess("发货完成");
        }catch (Exception e){
            e.printStackTrace();
            return RespBean.respError("参数异常：order_id = " + strOrder_id);
        }
    }
    //确认收货
    @Override
    public RespBean updateStatusByUser_id_0(HttpServletRequest req, String strOrder_id, String strCommodity_id, String strUser_id_0, String strUser_id_1) {
        try{
            int id = Integer.parseInt(strOrder_id);
            int commodity_id = Integer.parseInt(strCommodity_id);
            int user_id_0 = Integer.parseInt(strUser_id_0);
            int user_id_1 = Integer.parseInt(strUser_id_1);

            //获取到商品价格
            List<Commodity> commodities = commodityDao.getBeanById(commodity_id);
            double price = commodities.get(0).getPrice();
            //买家余额减少
            List<User> users = userDao.getBeanById(user_id_0);
            users.get(0).setMoney(users.get(0).getMoney() - price);
            int num = userDao.updateById(users.get(0));

            // 卖家余额增加
            List<User> users1 = userDao.getBeanById(user_id_1);
            users1.get(0).setMoney(users1.get(0).getMoney() + price);
            int num2 = userDao.updateById(users1.get(0));

            if (num == 0 || num2 == 0){
                return RespBean.respError("转账失败，数据库异常");
            }

            //订单更新订单状态
            int num3 = orderDao.updateStatusById(2, id);
            if (num == 0){
                return RespBean.respError("订单创建失败，数据库异常");
            }
            return RespBean.respSuccess("已确认收货");
        }catch (Exception e){
            e.printStackTrace();
            return RespBean.respError("参数异常：order_id,commodity_id,user_id_0,user_id_1 = " + strOrder_id +"," + strCommodity_id +"," + strUser_id_0 +"," + strUser_id_1);
        }
    }

    @Override
    public RespBean login(HttpServletRequest request, String name, String password) {
        return null;
    }

    @Override
    public RespBean register(Order order) {
        return null;
    }

    @Override
    public RespBean getPage(Object currentPage, Object len) {
        return null;
    }
}
